// pages/joke/joke.js
//传递全局变量
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newslist: [],
    loadingHide: false,
    hideFooter: true,
    page_text: 1,
    color: [],
    color_down: [],
    up_gray: '../image/ic_up_gray.png',
    up_red: '../image/ic_up_red.png',
    down_gray: '../image/ic_down_gray.png',
    down_red: '../image/ic_down_red.png'
  },
  changeEvent: function (e) {
    //注释代码无法赋值数据
    // console.log(e);
    // console.log(this.data.newslist);
    // var src = e.currentTarget.dataset.src;
    // var a=e.currentTarget.dataset.index;
    // console.log(this.data.newslist[a]);
    // var b=this.data.newslist[a].src;
    // this.setData({
    //   [b]:1
    // })
    // console.log(this.data.newslist[a]);
    var a = [];
    var b = [];
    a = this.data.color;
    b = this.data.color_down;
    if (e.currentTarget.dataset.tag == 1) {
      if (a[e.currentTarget.dataset.index] == 0) {
        b[e.currentTarget.dataset.index] = 0;
        a[e.currentTarget.dataset.index] = 1;
      } else
        a[e.currentTarget.dataset.index] = 0;
    } else {
      if (b[e.currentTarget.dataset.index] == 0) {
        a[e.currentTarget.dataset.index] = 0;
        b[e.currentTarget.dataset.index] = -1;
      } else
        b[e.currentTarget.dataset.index] = 0;
    }
    this.setData({
      color: a,
      color_down: b
    })
  },
  ClickEvent:function(){
    wx.showModal({
      title: '提示',
      content: '请下载APP进行分享操作',
      showCancel:false
    })
  },

  /**
   * 生命周期函数--监听页面加载k
   */
  onLoad: function (options) {
    // var that=this;
    // wx.request({
    //   url: 'https://api.tianapi.com/txapi/joke/index?key='+app.globalData.API+'&num='+app.globalData.NUMBER, 
    //   success: function (res) {
    //     console.log(res);
    //     if(res.data.code == 200){
    //     that.setData({
    //       newslist: res.data.newslist
    //     });
    //   }else{
    //       that.setData({
    //         newslist: res.data.msg
    //       }) 
    //   }
    //   },
    //   fail: function (err) {
    //     console.log(err)
    //   }
    // });
    //糗事百科API
    var that = this
    wx.request({
      url: 'http://m2.qiushibaike.com/article/list/text?type=refresh&page=1&count=' + app.globalData.NUMBER,
      success: function (res) {
        console.log(res);
        var a = [];
        var b = [];
        for (var i = 0; i < app.globalData.NUMBER; i++) {
          a.push(res.data.items[i].src);
          b.push(res.data.items[i].src);
        }
        that.setData({
          newslist: res.data.items,
          color: a,
          color_down: b,
          loadingHidden: true
        });
      }
    });

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (options) {
    var that = this;
    var page_text = this.data.page_text + 1;
    that.setData({
      page_text: page_text,
      loadingHidden: false
    });
    wx.request({
      url: 'http://m2.qiushibaike.com/article/list/text?type=refresh&page=' + page_text + '&count=12',
      success: function (res) {
        console.log(res);
        var items = res.data.items;
        var a = [];
        var b = [];
        for (var i = 0; i < app.globalData.NUMBER; i++) {
          a.push(res.data.items[i].src);
          b.push(res.data.items[i].src);
        }
        that.setData({
          newslist: that.data.newslist.concat(items),
          color: that.data.color.concat(a),
          color_down: that.data.color_down.concat(b),
          loadingHidden: true
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})